<template>
  <el-container class="home-container">
    <el-header>
      <div class="home-tudv">
        <!-- <img src="../assets/logo.png" alt="" /><br /> -->
        <h1>后台管理</h1>
      </div>
      <!-- 测试 -->
      <router-link :to="{ path: '/ceshi' }">测试</router-link>
      <el-button @click="logout" type="info">
        <!-- 编程跳转退出清空token -->退出
      </el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleColl">|||</div>
        <!-- router 
        开启路由 index 跳转
          unique-opened    持一个子菜单的展开
          :collapse="isCollapse"   是否水平折叠收起菜单  ：动态属性
          :collapse-transition="false"  禁用折叠动画
        -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#33b6a0"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 整个模板 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <!-- 一级菜单数据 -->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <!-- 
              :index=" '/'+ subitem.path + ''"
                前面 '/'+   是，跳转路径没有/ 得加上   数据库没有加/
                后面是  + ''"   element 框架 点击的时候唯一值，不至于点击一下二级菜单全部展开
              -->
            <el-menu-item
              :index="'/' + subitem.path"
              v-for="subitem in item.children"
              :key="subitem.id"
              @click="saveNavState('/' + subitem.path)"
            >
              <!-- 二级菜单数据 -->
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subitem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  export default {
    name: 'home',
    data() {
      return {
        menulist: [],
        iconsObj: {
          125: 'iconfont icon-yonghu',
          103: 'iconfont icon-12quanxianguanli',
          101: 'iconfont icon-shangpin',
          102: 'iconfont icon-shouye',
          145: 'iconfont icon-shujutongji',
        },
        isCollapse: true,
        activePath: '',
      }
    },
    created() {
      this.getMenlist()
      this.activePath = window.sessionStorage.getItem('activePath')
    },
    methods: {
      logout() {
        window.sessionStorage.clear()
        this.$router.push('/login')
      },
      async getMenlist() {
        const { data: res } = await this.$http.get('menus')
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.menulist = res.data
      },
      // 切换菜单的展开
      toggleColl() {
        this.isCollapse = !this.isCollapse
      },
      // 保存连接激活状态
      saveNavState(activePath) {
        window.sessionStorage.setItem('activePath', activePath)
        this.activePath = activePath
      },
    },
  }
</script>
<style lang="less" scoped>
  .home-container {
    height: 100%;
  }
  .el-header {
    background-color: #376483;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #ffffff;
    // color: #33b6a0;
    font-size: 20px;
    .home-tudv {
      display: flex;
      align-items: center;
      img {
        width: 100px;
        height: 100px;
      }
      span {
        margin-left: 15px;
      }
    }
  }
  .el-aside {
    user-select: none;
    background-color: #333744;
    .el-menu {
      border: none;
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
  .iconfont {
    margin-right: 10px;
    font-size: 20px;
  }
  .toggle-button {
    color: rgb(5, 3, 3);
    text-align: center;
    background-color: rgb(83, 106, 124);
    letter-spacing: 0.2em;
    line-height: 24px;
    cursor: pointer;
  }
</style>
